using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TangLiveRecorder.Models
{
    public class FileInfos
    {
        public string Guid { get; set; }
        public string Name { get; set; }
        public string Path { get; set; }

        public string Extension
        {
            get
            {
                return System.IO.Path.GetExtension(this.Path)?.ToLower();
            }
        }

        public string FileType
        {
            get
            {
                return GetFileType(this.Extension);
            }
        }

        public string FileIcon
        {
            get
            {
                return GetFileIcon(this.Extension);
            }
        }

        public long Size { get; set; }

        public string SizeFormatted
        {
            get
            {
                return FormatFileSize(this.Size);
            }
        }

        public DateTime CreatedTime { get; set; }
        public DateTime ModifiedTime { get; set; }
        public string Description { get; set; }

        public bool Exists
        {
            get
            {
                return System.IO.File.Exists(this.Path);
            }
        }

        private string GetFileType(string extension)
        {
            return extension?.ToLower() switch
            {
                ".mp4" or ".avi" or ".mkv" or ".mov" or ".wmv" or ".flv" => "视频文件",
                ".mp3" or ".wav" or ".flac" or ".aac" or ".ogg" => "音频文件",
                ".jpg" or ".jpeg" or ".png" or ".gif" or ".bmp" or ".webp" => "图片文件",
                ".txt" or ".log" or ".md" or ".rtf" => "文本文件",
                ".pdf" => "PDF文档",
                ".doc" or ".docx" => "Word文档",
                ".xls" or ".xlsx" => "Excel文档",
                ".ppt" or ".pptx" => "PowerPoint文档",
                ".zip" or ".rar" or ".7z" or ".tar" or ".gz" => "压缩文件",
                ".exe" or ".msi" => "可执行文件",
                _ => "未知文件"
            };
        }

        private string GetFileIcon(string extension)
        {
            return extension?.ToLower() switch
            {
                ".mp4" or ".avi" or ".mkv" or ".mov" or ".wmv" or ".flv" => "VideoCameraOutlined",
                ".mp3" or ".wav" or ".flac" or ".aac" or ".ogg" => "AudioOutlined",
                ".jpg" or ".jpeg" or ".png" or ".gif" or ".bmp" or ".webp" => "PictureOutlined",
                ".txt" or ".log" or ".md" or ".rtf" => "FileTextOutlined",
                ".pdf" => "FilePdfOutlined",
                ".doc" or ".docx" => "FileWordOutlined",
                ".xls" or ".xlsx" => "FileExcelOutlined",
                ".ppt" or ".pptx" => "FilePptOutlined",
                ".zip" or ".rar" or ".7z" or ".tar" or ".gz" => "FileZipOutlined",
                ".exe" or ".msi" => "SettingOutlined",
                _ => "FileOutlined"
            };
        }

        private string FormatFileSize(long bytes)
        {
            string[] sizes = { "B", "KB", "MB", "GB", "TB" };
            double len = bytes;
            int order = 0;
            while (len >= 1024 && order < sizes.Length - 1)
            {
                order++;
                len = len / 1024;
            }
            return $"{len:0.##} {sizes[order]}";
        }

        public override string ToString()
        {
            return $"{Name} - {SizeFormatted}";
        }
    }
}